/*
 * @Autor: mofish
 * @Date: 2022-08-25 10:17:49
 * @LastEditors: mofish
 * @LastEditTime: 2022-08-25 14:07:53
 * @Description: 下载 git 仓库模板
 */
const path = require("path");
const downloadGitRepo = require("download-git-repo");
const { TEMPLATE_PATH } = require("./constants");

const download = function (projectName) {
  /* 获取下载到本地的地址 */
  const downloadPath = path.join(process.cwd(), projectName);

  return new Promise((resolve, reject) => {
    downloadGitRepo(
      // 直连下载，默认下载master
      TEMPLATE_PATH,
      downloadPath,
      { clone: true },
      (error) => {
        if (!error) {
          resolve();
        } else {
          console.log(chalk.red("failed! 拉取模板失败", error));
          reject();
        }
      }
    );
  });
};

module.exports = download;
